<?php

namespace App\Http\Controllers\Admin;

use App\Http\Servers\Admin\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;

class WorkOrderController extends CommonController
{
    protected $jiajiEveryDay = 1;
    protected $ordernumErvryDay = 5;

    //工单信息列表
    public function index(Request $request, BaseServer $baseServer)
    {
        if ($request->id) {
            $workorder = \App\WorkOrder::findorfail($request->id);
            switch ($workorder->status) {
                case 0:
                    $workorder->status = 1;
                    $workorderStatus = '正在处理';
                    break;
                case 1:
                    $workorder->status = 2;
                    $workorderStatus = '处理完成';
                    break;
            }

            $result = $workorder->save();
            if ($result) {
                $baseServer->sendWorkOrderStatus($workorder);
                $message['message'] = message(true, '状态修改成功', '工单 ' . $workorder->order . ' 当前状态:' . $workorderStatus);
            } else {
                $message['message'] = message(false, '状态修改失败', '工单 ' . $workorder->order . ' 状态修改失败!');
            }

            return back()->with($message);
        }

        $data['workorderList'] = \App\WorkOrder::orderBy('created_at', 'DESC')
            ->get()
            ->toArray();

        $tmpOrderNum = \App\WorkOrder::where('created_at', '>', date('Y-m-d 00:00:00'))
            ->where('created_at', '<', date('Y-m-d 23:59:59'))
            ->count();

        if ($this->ordernumErvryDay - $tmpOrderNum > 0) {
            $data['ordernum'] = $this->ordernumErvryDay - $tmpOrderNum;
        }

        return view('admin.workorder.index', $data);
    }

    //添加工单
    public function workorder_add(Request $request, BaseServer $baseServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required',
                'type' => 'required',
                'connect' => 'required',
                'describe' => 'required',
            ]);
            $create['order'] = '#' . date('Ymd') . uniqid();
            $create['name'] = $request->name;
            $create['ip'] = getIp();
            $create['connect'] = $request->connect;
            $create['jiaji'] = $request->has('jiaji') ? 1 : 0;
            $create['type'] = $request->type;
            $create['status'] = 0;
            $create['describe'] = $request->describe;

            $result = \App\WorkOrder::create($create);
            if ($result) {
                $baseServer->sendWorkOrderToKf($create);
                $message['message'] = message(true, '工单提交成功', '工单编号:' . $create['order'] . '正在安排售后工作人员进行处理,请稍等!');
            } else {
                $message['message'] = message(false, '工单提交失败', '工单提交失败,请重新提交!');
            }

            return redirect()->route('admin.workorder.index')->with($message);
        }

        $tmpOrderNum = \App\WorkOrder::where('created_at', '>', date('Y-m-d 00:00:00'))
            ->where('created_at', '<', date('Y-m-d 23:59:59'))
            ->count();

        $tmpJiajiNum = \App\WorkOrder::where('created_at', '>', date('Y-m-d 00:00:00'))
            ->where('created_at', '<', date('Y-m-d 23:59:59'))
            ->count();

        if ($this->ordernumErvryDay - $tmpOrderNum <= 0) {
            $message['message'] = message(false, '工单数量过多', '当前提交的工单数量太多,请明天再试试吧~');
            return back()->with($message);
        }
        $data = [];
        if ($this->jiajiEveryDay - $tmpJiajiNum > 0) {
            $data['jiaji'] = $this->jiajiEveryDay - $tmpJiajiNum;
        }

        return view('admin.workorder.add', $data);
    }
}
